﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace StudentManage.Model
{
    public class MockStudentRepository : IStudentRepository
    {

        private List<Student> _students;

        public MockStudentRepository()
        {
            _students = new List<Student>()
            {
                new Student() { Id = 1, Name = "张三", ClassName = ClassNameEnum.FirstGrade, Email = "Tony-zhang@52abl.com"},
                new Student() { Id = 2, Name = "张四", ClassName = ClassNameEnum.FirstGrade, Email = "Tony-zhang44@52abl.com"},
                new Student() { Id = 3, Name = "张五", ClassName = ClassNameEnum.ThreeGrade, Email = "Tony-zhang555@52abl.com"},
            };
        }

        public Student Add(Student student)
        {
            student.Id = _students.Max(student => student.Id) + 1;
            _students.Add(student);
            return student;
        }

        public Student Delete(int id)
        {
            Student student = _students.FirstOrDefault(s => s.Id == id);
            if(student != null)
            {
                _students.Remove(student);
            }
            return student;
        }

        public IEnumerable<Student> GetAllStudents()
        {
            return _students;
        }

        public Student GetStudent(int id)
        {
            return _students.FirstOrDefault(a => a.Id == id);
        }

        public Student Update(Student updateStudent)
        {
            Student student = _students.FirstOrDefault(s => s.Id == updateStudent.Id);
            if(student != null)
            {
                student.Name = updateStudent.Name;
                student.Email = updateStudent.Email;
                student.ClassName = updateStudent.ClassName;
            }
            return student;
        }
    }
}
